﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using Draco.Commons.NamedObject.Configuration;

namespace Draco.Commons.NamedObject
{
    /// <summary>
    /// 对象缓冲池
    /// </summary>
    class ObjectCache : Hashtable
    {
        private static ObjectCache m_Instance = new ObjectCache();

        /// <summary>
        /// 实例
        /// </summary>
        public static ObjectCache Cache
        {
            get {return m_Instance;}
        }

        private ObjectCache()
            :base(StringComparer.CurrentCultureIgnoreCase)
        { 
        }
    }
    /// <summary>
    /// 配置对象缓存
    /// </summary>
    public class ObjectConfigCache
    {
        private static ObjectConfigCache m_Instance = new ObjectConfigCache();
        /// <summary>
        /// 实例
        /// </summary>
        public static ObjectConfigCache Cache
        {
            get { return m_Instance; }
        }

        private Hashtable m_Hash = new Hashtable(StringComparer.CurrentCultureIgnoreCase);

        private ObjectConfigCache()
        { 
        }

        /// <summary>
        /// 从Xml文件加载
        /// </summary>
        /// <param name="filePath"></param>
        public void LoadFromXml(string filePath)
        {
            List<NamedObjectItemConfiguration> list = NamedObjectConfigurationLoader.Load(filePath);
            Add(list);    
        }
        /// <summary>
        /// 从Xml文档加载
        /// </summary>
        /// <param name="xDoc"></param>
        public void LoadFromXml(XmlDocument xDoc)
        {
            List<NamedObjectItemConfiguration> list = NamedObjectConfigurationLoader.Load(xDoc);
            Add(list);
        }

        /// <summary>
        /// 添加配置项
        /// </summary>
        /// <param name="Item"></param>
        public void Add(NamedObjectItemConfiguration Item)
        {
            List<NamedObjectItemConfiguration>  list = new List<NamedObjectItemConfiguration>(1);
            list.Add(Item);
            this.Add(list);
        }
        /// <summary>
        /// 添加配置项
        /// </summary>
        /// <param name="list"></param>
        public void Add(List<NamedObjectItemConfiguration> list)
        {
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    if (m_Hash.ContainsKey(item.Name))
                    {
                        ObjectCache.Cache.Remove(item.Name);//清除缓存的对象
                    }
                    m_Hash[item.Name] = item;
                }
            }
        }

        /// <summary>
        /// 获取配置项
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public NamedObjectItemConfiguration this[string Name]
        {
            get { return m_Hash[Name] as NamedObjectItemConfiguration; }
        }

        /// <summary>
        /// 获取数目
        /// </summary>
        public int Count
        {
            get { return m_Hash.Count; }
        }

        /// <summary>
        /// 清除
        /// </summary>
        public void Clear()
        {
            this.m_Hash.Clear();
        }
    }
}
